package nio.nio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

/**
 * @author 帅小伙
 * @date 2022/1/14
 * @description
 * NIO的客户端
 */
public class NIOClient {

    public static void main(String[] args) throws Exception {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.configureBlocking(false);
        InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 6666);

        // 连接服务器
        if(!socketChannel.connect(inetSocketAddress)){
            while(!socketChannel.finishConnect()){
                System.out.println("因为连接需要时间  所以干些其他事情");
            }
        }

        String str = "hello ah";
        ByteBuffer byteBuffer = ByteBuffer.wrap(str.getBytes(),0,str.getBytes().length);
        socketChannel.write(byteBuffer);
        // 代码会停在这
        System.in.read();
    }
}
